<!--
/// 
/// MaDAson - Toolkit for Model Driven Software Development
///
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
-->
<%@ Page Language="C#" %>
<%@ Import Namespace="Madason.MetaModel" %>
<%@ Import Namespace="Madason.MetaModel.DAO" %>
package <%= Package %>;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import <%= Class.Namespace.FullName %>.*;

import net.sf.click.Page;
import net.sf.click.control.ActionLink;
import net.sf.click.control.Column;
import net.sf.click.control.Table;
import net.sf.click.extras.control.LinkDecorator;

public class <%= Class.Name %>ListPage extends Page 
{
	Table table;
	ActionLink editLink;
	
	public <%= Class.Name %>ListPage()
	{
		editLink = new ActionLink("edit", this, "onEdit");
		
		table = new Table("table");
		table.setSortable(true);
		table.setPageSize(20);
		
<% foreach (Madason.MetaModel.Attribute attr in Class.Attributes) { %>		
		table.addColumn(getColumn<%= attr.NamePascalCase %>());
<% } %>		


		
		Column editColumn = new Column("");
		editColumn.setDecorator(new LinkDecorator(table, editLink, "<%= Class.PrimaryKey.Name %>"));
		table.addColumn(editColumn);
		
		addControl(table);	
		addControl(editLink);
	}
	
<% foreach (Madason.MetaModel.Attribute attr in Class.Attributes) { %>			
	protected Column getColumn<%= attr.NamePascalCase %>()
	{
		Column col = new Column("<%= attr.Name %>");
<%   if (attr.Type.Name == "Date") { %>		
		col.setFormat("{0,date,dd.MM.yyyy}");
<%   } %>				
		return col;
	}	
	
<% } %>	

	@Override
	public void onInit()
	{		
		DataSource datasource = null;
		Connection conn = null;
		
		try
		{
			conn = DBUtil.getConn();
			
			<%= Class.Name %>Access access = <%= Class.Name %>Access.getInstance();
			List<%= "<" + Class.ExplicitName + ">"%> list = access.findAll(conn);
			table.setRowList(list);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (conn != null)
				try
				{
					conn.close();
				} 
				catch (SQLException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}
		
		super.onInit();
	}
	
	public boolean onEdit()
	{
		setForward("<%= Class.NameCamelCase %>Edit.htm?loadid="+editLink.getValue());
		return true;
	}
}






<script runat="server">
	private Entity @class;
	private string package;

	public string Package
	{
		get { return package; }
		set { package = value; }
	}
	
	public PersistedAttribute PrimaryKey
	{
		get
		{
			return this.Class.PrimaryKey;
		}
	}

	public Entity Class
	{
		get { return @class; }
		set { @class = value; }
	}

</script>